clear all
clc
n = input('number of classes: ');
%generation the probabilities array
if mod(n,2)== 0
    x=n/2;
    alfa=1/(x*(x+1));
else x=n/2 - 0.5;
    alfa=1/(x+1)^2;
    P(x+1)=alfa*(x+1);
end
for i=1:x
    P(i)=alfa*i;
    P(n-i+1)=alfa*i;
end
%filling the matrix A1
A1 = zeros(n,n+1);
A1(1,1) = -P(1);
A1(n,1)= P(1);
for i=2:n
    A1(i,i )= -P(i);
    A1(i-1,i) = P(i);
end 
lambdav=8;%car arrival rates %should convert it to a poisson 
A1=lambdav*A1;
for i=1:n;
 A1(i,n+1)=-1;
end
A1=-A1;
for i=1:n
Probabilities(i,1)=P(i); 
end
lambdavi=lambdav*Probabilities;
Lambdac=4;%suppose like cars arrival , one arrival devided to categories with the same probabilities
Lambdaci=Lambdac*Probabilities;
b1=lambdavi-Lambdaci;
c=zeros(n,1);c(n+1)=-1;

%build the charging stations constraint 
A2=A1;
for i=1:n
A2(i,n+1)=0;
end
%constaint foe deplted cars 
A3=A2;
A3(n,1)=0;
for i=1:n
A3(i,i+1)=0;
end
muc=4;
b3=ones(n,1)*muc - lambdavi;
A4=zeros(1,n+1);
A4(1,1)=lambdav*P(1);
b4=ones(1,1)*muc/n;
%total contraint
% A=A1;
% b=b1;
A=[A1;A2;A3;A4];
b=[b1;b1;b3;b4];

%there is no equalities constrainrs so we put them on 0
Aeq=zeros(n,n+1);
beq=zeros(n,1);
%upper and lowe bound of X
lb=zeros(n+1,1);
ub=ones(n,1);
ub(n+1,1)=-log(0);
[xsol,fval,exitflag,output,lamd] = linprog(c,A,b,Aeq,beq,lb,ub);
Af=-A1(1:n,1:n)*xsol(1:n)+b1 ;
%single waiting time
for i=1:n
Af(i,1)=1/Af(i,1);
end
%total waitinf time 
W=0;
for i=1:n
W=W+Af(i,1);
end

